import os, ConfigParser

try:
    config = ConfigParser.RawConfigParser()
    print 'constantsdir:', os.getcwd()
    config.read('./Roomba/temp-settings.cfg')
    # load agent settings
    AGENT_TYPE = config.get('agent', 'type')
    AGENT_AMOUNT = config.getint('agent', 'amount')
    # load experiment settings
    WINDOWLESS = config.getboolean('experiment', 'headless')
    SAVEFILE = config.get('experiment', 'savefile')
    GUI_ON = config.getboolean('experiment', 'gui_in_screen')
    EVALUATE = config.getboolean('experiment', 'evaluate')
    EVAL_INTERVAL = config.getint('experiment', 'evaluation_interval')
    EXP_EPISODES = config.getint('experiment', 'episodes')
    EXP_EXPERIMENTS = config.getint('experiment', 'experiments')
    # load world settings
    GRID_WIDTH = config.getint('world','grid_width')
    GRID_HEIGHT = config.getint('world', 'grid_height')
    N_GRID = GRID_HEIGHT * GRID_HEIGHT
    MOVE_PENALTY = config.getfloat('world', 'move_penalty')
    FIXED_SEED = config.getboolean('world', 'fixed_seed')
    NO_CRUMBS = config.getint('world', 'crumbs')
    SENSE_RADIUS = config.getint('world', 'sense_radius')
    if SENSE_RADIUS == 0:
        VISIBLE_CELLS = N_GRID
    elif SENSE_RADIUS == 1:
        VISIBLE_CELLS = 1 # nr of cells you can sense
    elif SENSE_RADIUS == 2:
        VISIBLE_CELLS = 5
    elif SENSE_RADIUS == 3:
        VISIBLE_CELLS = 9
    else:
        VISIBLE_CELLS = 13
    END_STATE = config.getboolean('world', 'end_state')
    s = config.get('world', 'seed')
    if not FIXED_SEED or s == 'None':
        SEED = None
    else:
        SEED = int(s)
    STEPS_PER_EPISODE = config.getint('world', 'no_steps')
    # load action settings
    JOINT_ACTION = False#config.get('action', 'joint_action')
    ACTION_TYPE = config.get('action', 'action_type')
    EMPTY_CELL = config.get('action', 'empty_cell')
    NSEW_ACTIONS = config.getboolean('action', 'nsew_actions')
    RECONSIDER_STUCK = config.getboolean('action', 'reconsider_stuck')
    TIME_STUCK = config.getint('action', 'time_stuck')
    LAST_STUCK_COUNT = config.getint('action', 'last_stuck_count')
    COORDINATE_TARGETS = config.getboolean('action', 'coordinate_targets')
    # load q-learning settings
    ANYTIME_UPDATE = config.getboolean('qlearning', 'anytime_update')
    EPSILON = config.getfloat('qlearning', 'epsilon')
    ALPHA = config.getfloat('qlearning', 'alpha')
    GAMMA = config.getfloat('qlearning', 'gamma')
    FINAL_REWARD = config.getint('qlearning', 'final_reward')

    # Set file header to test existing results files
    
except Exception as e:
    print 'No settings file (or missing value), please add "settings.cfg" and try again'
    raise e




# initialize global constants #
XDIM = 200.0
YDIM = 200.0
HEIGHT = 20.0
OFFSET = -HEIGHT/2
MAX_SPEED = 1 # max per-step translational speed
ROOMBA_RAD = 6  # Physical Radius of Roomba, for vacuuming and collisions
DISTANCE_REACHED = ROOMBA_RAD
MOE = 0      # Margin Of Error
OBJECT_TYPE_ROOMBA = (1 << 0)
OBJECT_TYPE_WALLS = (1 << 1)
OBJECT_TYPE_FLOOR = (1 << 2)
OBJECT_TYPE_MARKER = (1 << 3)
# Extension constants
Q_EXT = '.qtable'
RES_EXT = '.result'
SETTINGS_EXT = '.settings'
# GRID CONSTANTS
START_GRID_SENSORS = 0
GRID_SENSORS = 2

ACTION_TYPES = ['closest', 'central'] # What action types are there to choose from
RELATIVE_BELIEF = True

DISCRETE_CRUMB_LEVELS = [5, 20]

SAME_STATES = True
#                      orig, hor, ver, bl-tr, tl-br, 90, 180, 270
ACTION_TRANSFORM_3 = {  0:[   0,   6,   2,     8,     0,  2,   8,   6],\
                        1:[   1,   7,   1,     5,     3,  5,   7,   3],\
                        2:[   2,   8,   0,     2,     6,  8,   6,   0],\
                        3:[   3,   3,   5,     7,     1,  1,   5,   7],\
                        4:[   4,   4,   4,     4,     4,  4,   4,   4],\
                        5:[   5,   5,   3,     1,     7,  7,   3,   1],\
                        6:[   6,   0,   8,     6,     2,  0,   2,   8],\
                        7:[   7,   1,   7,     3,     5,  3,   1,   5],\
                        8:[   8,   2,   6,     0,     8,  6,   0,   2]}

ACTION_TRANSFORM_5 = {  0:[   0,  20,   4,    24,     0,  4,  24,  20],\
                        1:[   1,  21,   3,    19,     5,  9,  23,  15],\
                        2:[   2,  22,   2,    14,    10, 14,  22,  10],\
                        3:[   3,  23,   1,     9,    15, 19,  21,   5],\
                        4:[   4,  24,   0,     4,    20, 24,  20,   0],\
                        5:[   5,  15,   9,    23,     1,  3,  19,  21],\
                        6:[   6,  16,   8,    18,     6,  8,  18,  16],\
                        7:[   7,  17,   7,    13,    11, 13,  17,  11],\
                        8:[   8,  18,   6,     8,    16, 18,  16,   6],\
                        9:[   9,  19,   5,     3,    21, 23,  15,   1],\
                       10:[  10,  10,  14,    22,     2,  2,  14,  22],\
                       11:[  11,  11,  13,    17,     7,  7,  13,  17],\
                       12:[  12,  12,  12,    12,    12, 12,  12,  12],\
                       13:[  13,  13,  11,     7,    17, 17,  11,   7],\
                       14:[  14,  14,  10,     2,    22, 22,  10,   2],\
                       15:[  15,   5,  19,    21,     3,  1,   9,  23],\
                       16:[  16,   6,  18,    16,     8,  6,   8,  18],\
                       17:[  17,   7,  17,    11,    13, 11,   7,  13],\
                       18:[  18,   8,  16,     6,    18, 16,   6,   8],\
                       19:[  19,   9,  15,     1,    23, 21,   5,   3],\
                       20:[  20,   0,  24,    20,     4,  0,   4,  24],\
                       21:[  21,   1,  23,    15,     9,  5,   3,  19],\
                       22:[  22,   2,  22,    10,    14, 10,   2,  14],\
                       23:[  23,   3,  21,     5,    19, 15,   1,   9],\
                       24:[  24,   4,  20,     0,    24, 20,   0,   4]}


########################### Plan of the room ###################################
"""
   y=0                                               200
x=0 +------------------------------------+-----------+
    |                               #### |           |
    |####                           #### |           |
    |####                           #### +-----======|          ANGLE:
    |####                           ####             |             180
    |#########              ############             |              |
    |#########              ############             |         270--O-- 90
    |#########              ############             |              |
    |####                                            |              0
    |####                                            |
    |####                                            |
    |####                                            |
    |####                                            |
    |####         ####                  ####         |
    |             ####                  ####         |
    |             ####                  ####         |
    |       #########################################|
    |       #########################################|
    |       #########################################|
200 +------------------------------------------------+
"""




